﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Programacion3_TPFinal_LosBorbotones
{
    public partial class Form3 : Form
    {
        DataSet dsYamabushi;
        Datos obDatos = new Datos();

        string rutaBD = "Data Source=localhost;Initial Catalog=Yamabushi;Integrated Security=True";
            
        public Form3()
        {
            InitializeComponent();
        }

       

        private void Form3_Load(object sender, EventArgs e)
        {
            string ConsultaSQLAlumnos = " select * from cuotas";
            dsYamabushi = new DataSet();

            AgregarTablaDataSetSQLServer(rutaBD, ConsultaSQLAlumnos,
                                         "cuotas", ref dsYamabushi);

            Grilla.DataSource = dsYamabushi.Tables[0];
            Grilla.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AgregarTablaDataSetSQLServer(string conexionBD, string ConsultaSQL,
                                string NombreTabla, ref DataSet ds)
        {
            SqlConnection cn = new SqlConnection(conexionBD);
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            adaptador.Fill(ds, NombreTabla);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Grilla_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (int count = 0; count < Grilla.Rows.Count; count++)
            {
                if (Convert.ToInt32(this.Grilla.Rows[count].Cells[4].Value) == 1)
                {
                    Grilla.Rows[count].DefaultCellStyle.BackColor = System.Drawing.Color.LightGreen;
                }
                else
                {
                    Grilla.Rows[count].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string habilitar = "UPDATE cuotas set pagado=1 where Dni_Alumno =" + Grilla.CurrentRow.Cells[0].Value.ToString() + "and cod_curso =" + Grilla.CurrentRow.Cells[1].Value.ToString() + "and cod_sede=" + Grilla.CurrentRow.Cells[2].Value.ToString() + " ";
            string agregarfechapago = "UPDATE Cuotas set fechadepago=getdate() where Dni_Alumno =" + Grilla.CurrentRow.Cells[0].Value.ToString() + "and cod_curso =" + Grilla.CurrentRow.Cells[1].Value.ToString() + "and cod_sede=" + Grilla.CurrentRow.Cells[2].Value.ToString() + " ";
                obDatos.consultar(habilitar, "cuotas");
                obDatos.consultar(agregarfechapago, "cuotas");
                this.Close();
            MessageBox.Show("Pago realizado !!!");
                
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ClausulaSQLProductos = "";
            if (txtDni.Text != "")
                ConstruirClausulaSQL("Dni_Alumno",
                                     txtDni.Text,
                                     ref ClausulaSQLProductos);
            dsYamabushi.Tables.Clear();

            AgregarTablaDataSetSQLServer(rutaBD,
                      "Select * from cuotas " + ClausulaSQLProductos + " order by dni_alumno",
                                    "Consulta",
                                    ref dsYamabushi);

            Grilla.DataSource = dsYamabushi.Tables["Consulta"];

        }

        private void Grilla_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Grilla_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            button2.Enabled = true;
        }

        private void ConstruirClausulaSQL(string NombreCampo,

                                           string Valor,
                                           ref string Clausula)
        {
            string d1 = " = ";  //Delimitador 1
            //string d2 = ""; //Delimitador 2
            if (Clausula == "")
                Clausula = Clausula + " WHERE ";
            else
                Clausula = Clausula + " AND ";

            Clausula =
                Clausula + NombreCampo + d1 + Valor;
        } // CONSTRUIRCLAUSULA

 
    }
}
